package com.ruoyi.project.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GetLoginUser;
import com.ruoyi.member.domain.MessageReceive;
import com.ruoyi.member.domain.MessageSend;
import com.ruoyi.member.mapper.MessageReceiveMapper;
import com.ruoyi.member.mapper.MessageSendMapper;
import com.ruoyi.member.service.impl.MessageSendServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ProjectRiskMapper;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.service.IProjectRiskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目风险Service业务层处理
 * 
 * @author renyuxin
 * @date 2024-06-05
 */
@Service
public class ProjectRiskServiceImpl implements IProjectRiskService 
{
    @Autowired
    private ProjectRiskMapper projectRiskMapper;

    @Autowired
    private MessageSendMapper messageSendMapper;

    @Autowired
    private MessageReceiveMapper messageReceiveMapper;

    /**
     * 查询项目风险
     * 
     * @param riskId 项目风险主键
     * @return 项目风险
     */
    @Override
    public ProjectRisk selectProjectRiskByRiskId(Long riskId)
    {
        return projectRiskMapper.selectProjectRiskByRiskId(riskId);
    }

    /**
     * 查询项目风险列表
     * 
     * @param projectRisk 项目风险
     * @return 项目风险
     */
    @Override
    public List<ProjectRisk> selectProjectRiskList(ProjectRisk projectRisk)
    {
        return projectRiskMapper.selectProjectRiskList(projectRisk);
    }

    /**
     * 新增项目风险
     * 
     * @param projectRisk 项目风险
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int insertProjectRisk(ProjectRisk projectRisk)
    {
        int i = projectRiskMapper.insertProjectRisk(projectRisk);

        // 向 跟进人 发送一个通知
        if (i > 0){
            // 获取当前登录用户id
            Long loginId = GetLoginUser.id();
            // 添加 通知消息
            MessageSend messageSend = new MessageSend();
            messageSend.setProjectId(projectRisk.getProjectId());
            messageSend.setMessageContent("项目风险通知:您的项目有新的"+projectRisk.getRiskTitle() +",请注意查看" );
            messageSend.setMessageDate(DateUtils.getNowDate());
            messageSend.setUserId(loginId);
            // 如果添加成功添加我的消息,设置接收人为跟进人
            if (messageSendMapper.insertMessageSend(messageSend) > 0) {
                MessageReceive messageReceive = new MessageReceive();
                messageReceive.setMessageId(messageSend.getMessageId());
                messageReceive.setMessageMyStatus(1);
                messageReceive.setProjectId(projectRisk.getProjectId());
                messageReceive.setUserId(projectRisk.getUserId());
                int i1 = messageReceiveMapper.insertMessageReceive(messageReceive);
            }
        }
        return i;
    }

    /**
     * 修改项目风险
     * 
     * @param projectRisk 项目风险
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int updateProjectRisk(ProjectRisk projectRisk)
    {
        return projectRiskMapper.updateProjectRisk(projectRisk);
    }

    /**
     * 批量删除项目风险
     * 
     * @param riskIds 需要删除的项目风险主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectRiskByRiskIds(Long[] riskIds)
    {
        return projectRiskMapper.deleteProjectRiskByRiskIds(riskIds);
    }

    /**
     * 删除项目风险信息
     * 
     * @param riskId 项目风险主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteProjectRiskByRiskId(Long riskId)
    {
        return projectRiskMapper.deleteProjectRiskByRiskId(riskId);
    }

    @Override
    public List<ProjectRisk> getRiskCount(Long projectId) {
        return projectRiskMapper.selectRiskByProjectId(projectId);
    }
}
